import { ipcMain } from 'electron'
import { MQTTService } from './mqttService'

// MQTT IPC处理器类
export class MQTTIPCHandler {
  private mqttService: MQTTService

  constructor(mqttService: MQTTService) {
    this.mqttService = mqttService
    this.setupIPCHandlers()
  }

  // 设置IPC处理器
  private setupIPCHandlers(): void {
    // 初始化MQTT连接
    ipcMain.handle('mqtt-initialize', async (_event, brokerUrl: string, username: string, password: string, clientId: string) => {
        try {
          await this.mqttService.initialize(brokerUrl, username, password, clientId)
          return { success: true }
        } catch (error) {
          console.error('MQTT初始化失败:', error)
          return { success: false, error: error instanceof Error ? error.message : '未知错误' }
        }
      }
    )

    // 断开MQTT连接
    ipcMain.handle('mqtt-disconnect', async () => {
      try {
        this.mqttService.disconnect()
        return { success: true }
      } catch (error) {
        console.error('MQTT断开连接失败:', error)
        return { success: false, error: error instanceof Error ? error.message : '未知错误' }
      }
    })

    // 重新连接
    ipcMain.handle('mqtt-reconnect', async () => {
      try {
        await this.mqttService.reconnect()
        return { success: true }
      } catch (error) {
        console.error('MQTT重连失败:', error)
        return { success: false, error: error instanceof Error ? error.message : '未知错误' }
      }
    })

    // 获取MQTT状态
    ipcMain.handle('mqtt-get-status', async () => {
      try {
        return this.mqttService.getServiceStatus()
      } catch (error) {
        console.error('获取MQTT状态失败:', error)
        return {
          isInitialized: false,
          isConnected: false,
          isAvailable: false
        }
      }
    })

    // 获取应用信息
    ipcMain.handle('mqtt-get-app-info', async () => {
      try {
        return this.mqttService.getAppInfo()
      } catch (error) {
        console.error('获取应用信息失败:', error)
        return null
      }
    })

    // 请求应用信息
    ipcMain.handle('mqtt-request-app-info', async () => {
      try {
        this.mqttService.requestAppInfo()
        return { success: true }
      } catch (error) {
        console.error('请求应用信息失败:', error)
        return { success: false, error: error instanceof Error ? error.message : '未知错误' }
      }
    })

    // 获取警告状态
    ipcMain.handle('mqtt-get-warn-status', async () => {
      try {
        return this.mqttService.getWarnStatus()
      } catch (error) {
        console.error('获取警告状态失败:', error)
        return null
      }
    })

    // 请求警告状态
    ipcMain.handle('mqtt-request-warn-status', async () => {
      try {
        this.mqttService.requestWarnStatus()
        return { success: true }
      } catch (error) {
        console.error('请求警告状态失败:', error)
        return { success: false, error: error instanceof Error ? error.message : '未知错误' }
      }
    })
  }

  // 清理IPC处理器
  cleanup(): void {
    ipcMain.removeHandler('mqtt-initialize')
    ipcMain.removeHandler('mqtt-disconnect')
    ipcMain.removeHandler('mqtt-reconnect')
    ipcMain.removeHandler('mqtt-get-status')
    ipcMain.removeHandler('mqtt-get-app-info')
    ipcMain.removeHandler('mqtt-request-app-info')
    ipcMain.removeHandler('mqtt-get-warn-status')
    ipcMain.removeHandler('mqtt-request-warn-status')
  }
}
